<?php

class About extends Controller {

    function About()
    {
        parent::Controller();    
        if(!admin_session_check()) redirect('admin/login');
        if(!is_allowed("all")) show_404();
        $this->load->model('aboutmodel');  
        $this->load->library("form_validation");       
    }     
    
    function index()
    {

        $data["list"] = $this->aboutmodel->admin_get_list();
        $data["response"] = get_response();        

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'about'));
        $this->load->view("admin/default/quick_search");
        $this->load->view('admin/about/about_list', $data);   
        $this->load->view('admin/default/footer');   

    }
 
    function edit($id = NULL)
    {
        if($id == NULL) show_404();

        $data["item"] = $this->aboutmodel->admin_get_item($id); 
        if(empty($data["item"])) show_404();

        $this->_process_form();

        $data["action"] = "update";
        $data["token"] = generate_token("about",720);

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'about'));
        $this->load->view('admin/about/about_form', $data);   
        $this->load->view('admin/default/footer');   

    }
       
    function add()
    {
        $this->_process_form();

        $data["item"] = array(
                "id_About" => "",   
                "Title_About" => "", 
                "Introduction_About" => "",
                "Description_About" => ""
        ); 

        $data["action"] = "create";
        $data["token"] = generate_token("about",720);

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'about'));
        $this->load->view('admin/about/about_form', $data);   
        $this->load->view('admin/default/footer');   

    }            
    function remove($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->aboutmodel->admin_get_item($id, "id_About as id, Title_About as title, Introduction_About as intro, Description_About as description");

        if(empty($data["item"])){

            set_response('error', "The item you're attempting to delete does not exist");
            redirect('admin/about');

        } else {

            $data["controller"] = "about";
            $this->load->model('glossarymodel');

            $this->load->view('admin/default/head');          
            $this->load->view('admin/default/header', array('selected'=>'pages'));  
            $this->load->view("admin/default/nav_pages", array('selected' => 'about')); 
            $this->load->view('admin/about/about_delete', $data);   
            $this->load->view('admin/default/footer');   

        }
    }  
    function preview($id = NULL)
    {

        if($this->input->post("submit_action") === FALSE){

            $data["item"] = $this->aboutmodel->admin_get_item($id, "id_About as id, Title_About as title, Introduction_About as intro, Description_About as description"); 
            if (empty($data["item"])) show_404();             

            $data["action"] = "view";

        } else {

            $data["item"] = array(
                'id' => $this->input->post("id", TRUE),      
                'title' => $this->input->post("title", TRUE),  
                'intro' => $this->input->post("introduction", TRUE),
                'description' => $this->input->post("description", TRUE) );

            $data["action"] = "preview";

        }

        $data["controller"] = "about";
        $data["headlines"] = $this->aboutmodel->get_about_headlines(); 

        $this->load->model('glossarymodel');
        $this->load->view('admin/previews/preview_about_template', $data);   

    }
    function confirm_removal($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->aboutmodel->admin_get_item($id);

        if(empty($data["item"])){

            set_response('error', "The item you're attempting to delete does not exist");
            redirect('admin/about');

        } else if($id == ABOUT_DEFAULT_ID){

            set_response('error', "You can't delete the default item");
            redirect('admin/about');

        } else {

            $this->aboutmodel->admin_delete($id);
            set_response('success', "The item <i>".$data["item"]["Title_About"]."</i> was deleted.");
            redirect('admin/about');
        }
    }
    function _process_form()
    {        
        if($this->input->post("submit_action") !== FALSE){

            if ($this->_validate_form()) {

                if (validate_token("about",$this->input->post("token",TRUE))){

                    $data = array(
                        'Introduction_About' => $this->input->post("introduction"),
                        'Description_About' => $this->input->post("description")
                    );

                    if ($this->input->post("id") != ABOUT_DEFAULT_ID) $data['Title_About'] = $this->input->post("title");

                    switch($this->input->post("submit_action")){    

                        case 'create':

                            $new_id = $this->aboutmodel->admin_insert($data);
                            set_response('success','The item <i>'.$data['Title_About'].'</i>. was created. <a href="'.site_url("admin/about/preview/".$new_id).'" rel="view">Click to view</a>');

                        break;
                        case 'update':

                            $this->aboutmodel->admin_update($this->input->post("id"), $data);
                            set_response('success','The item <i>'.$data['Title_About'].'</i> was updated. <a href="'.site_url("admin/about/preview/".$this->input->post("id")).'" rel="view">Click to view</a>');

                        break;        

                    }

                    redirect("admin/about");

                }
            }   
        }
    }

    private function _validate_form(){
        
        $config[] = array();

        if ($this->input->post("id") != ABOUT_DEFAULT_ID){

            $config[] = array( 
                         'field'   => 'title',
                         'label'   => 'Title',
                         'rules'   => 'trim|required|xss_clean|callback_check_duplicated|max_length[250]'
                        );
        }

            $config[] = array( 
                         'field'   => 'introduction',
                         'label'   => 'Introduction',
                         'rules'   => 'trim|xss_clean'
                        );
             $config[] = array(
                         'field'   => 'description',
                         'label'   => 'Description',
                         'rules'   => 'trim|required|xss_clean'
                        );
        
                                                 
        $this->form_validation->set_rules($config);       
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        
        if ($this->form_validation->run($this) == FALSE){
            return FALSE;
        }else
            return TRUE;

    }

     public function check_duplicated($name){ 

        $id = $this->input->post("id");

        if($this->aboutmodel->is_duplicated('Title_About',$name,$id)){
            $this->form_validation->set_message('check_duplicated', "That title already exists.");
            return FALSE;
        }else{
            return TRUE;    
        }
    } 

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */